package cn.org.mrliu.ssm.web.controller;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;

import cn.org.mrliu.ssm.common.web.controller.BaseController;
import cn.org.mrliu.ssm.common.web.vo.ResponseMessage;
import cn.org.mrliu.ssm.web.param.UserParam;

@Controller
@RequestMapping("/upload")
public class UploadImageController extends BaseController {

	@RequestMapping("/uploadImage")
	@ResponseBody
	public ResponseMessage upload(@RequestParam("file_data") MultipartFile file) throws Exception {
		String newFileName = null;
		ResponseMessage responseMessage = new ResponseMessage();
		try {
			System.out.println("upload....");
			System.out.println(file);
			System.out.println(file.getContentType());
			System.out.println(file.getName());
			System.out.println(file.getOriginalFilename());
			System.out.println(file.getSize());
			System.out.println(file.isEmpty());
			System.out.println(file);
			newFileName = System.currentTimeMillis() + file.getOriginalFilename();
			File destFile = new File("D:\\testUpload\\" + newFileName);
			file.transferTo(destFile);

			Map<String, Object> map = new HashMap<>();
			map.put("id", null);
			map.put("url", newFileName);
			map.put("name", file.getOriginalFilename());
			responseMessage.setData(map);
			responseMessage.setMessage("上传成功!");
		} catch (Exception e) {
			e.printStackTrace();
			responseMessage.setSuccess(false);
			responseMessage.setMessage("上传失败!");
		}

		return responseMessage;
	}

	@RequestMapping("/batchUploadImage")
	@ResponseBody
	public String batchUpload(@RequestParam("file_data") MultipartFile[] files) throws Exception {
		System.out.println("batchUpload....");
		for (MultipartFile file : files) {
			System.out.println(file);
			System.out.println(file.getContentType());
			System.out.println(file.getName());
			System.out.println(file.getOriginalFilename());
			System.out.println(file.getSize());
			System.out.println(file.isEmpty());
			System.out.println(file);
			File destFile = new File("D:\\testUpload\\" + System.currentTimeMillis() + file.getOriginalFilename());
			file.transferTo(destFile);
		}

		/**
		 * [{ "id":1, "name":"", "url":"" },{ "id":1, "name":"", "url":"" } ]
		 */

		return "OK";
	}

	@RequestMapping("/test")
	@ResponseBody
	public ResponseMessage test(@RequestBody UserParam userParam) throws Exception {
		try {
			System.out.println("test....");
			System.out.println(userParam);
			System.out.println(JSON.toJSONString(userParam, true));

			return new ResponseMessage("成功!");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ResponseMessage(false, "失败!");
	}

	@RequestMapping("/test1")
	@ResponseBody
	public ResponseMessage test1(String formParam) throws Exception {
		try {
			System.out.println("test....");
			System.out.println(formParam);
			UserParam userParam = JSON.parseObject(formParam, UserParam.class);
			System.out.println(JSON.toJSONString(userParam, true));

			return new ResponseMessage("成功!");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ResponseMessage(false, "失败!");
	}

}
